<?php
/**
 * 
 * IndexAction.class.php (前台首页)
 *
 * @package      	jgsat
 * @author          liuxun QQ:147613338 <admin@jgsat.cn>
 * @copyright     	Copyright (c) 2008-2011  (http://www.jgsat.com)
 * @license         http://www.jgsat.com/license.txt
 * @version        	jgsat企业网站管理系统 v2.1 2011-03-01 jgsat.cn $
 */
if(!defined("JGsat")) exit("Access Denied");
class IndexAction extends BaseAction
{
    public function index()
    {

        $user_agent = $_SERVER['HTTP_USER_AGENT'];
    	$mobile_agents = Array("240x320","acer","acoon","acs-","abacho","ahong","airness","alcatel","amoi","android","anywhereyougo.com","applewebkit/525","applewebkit/532","asus","audio","au-mic","avantogo","becker","benq","bilbo","bird","blackberry","blazer","bleu","cdm-","compal","coolpad","danger","dbtel","dopod","elaine","eric","etouch","fly ","fly_","fly-","go.web","goodaccess","gradiente","grundig","haier","hedy","hitachi","htc","huawei","hutchison","inno","ipad","ipaq","ipod","jbrowser","kddi","kgt","kwc","lenovo","lg ","lg2","lg3","lg4","lg5","lg7","lg8","lg9","lg-","lge-","lge9","longcos","maemo","mercator","meridian","micromax","midp","mini","mitsu","mmm","mmp","mobi","mot-","moto","nec-","netfront","newgen","nexian","nf-browser","nintendo","nitro","nokia","nook","novarra","obigo","palm","panasonic","pantech","philips","phone","pg-","playstation","pocket","pt-","qc-","qtek","rover","sagem","sama","samu","sanyo","samsung","sch-","scooter","sec-","sendo","sgh-","sharp","siemens","sie-","softbank","sony","spice","sprint","spv","symbian","tablet","talkabout","tcl-","teleca","telit","tianyu","tim-","toshiba","tsm","up.browser","utec","utstar","verykool","virgin","vk-","voda","voxtel","vx","wap","wellco","wig browser","wii","windows ce","wireless","xda","xde","zte");

    	$is_mobile = false;
        foreach ($mobile_agents as $device) {
            if (stristr($user_agent, $device)) {

                $is_mobile = true;

                break;
            }
        }




	    //print_r($_COOKIE);exit;
        $code=$_GET['code'];
        $state=$_GET['state'];
//echo 'sss';exit;
        if($code){
            if($is_mobile){
                $this->redirect('Mobile/Oauth/qqcallback', array('code'=>$code,'state'=>$state));
            }else {
                $this->redirect('User/Oauth/qqcallback', array('code' => $code, 'state' => $state));
            }
        }else {


            if($is_mobile){
                header("Location: ".U('Mobile/Index/index'));
            }


            $this->assign('bcid', 0);//顶级栏目
            $this->assign('ishome', 'home');
            //echo $seo_title;exit;
            $this->display();
        }
    }
 
   
 
    public function magazine(){
        $this->display();
    }

    public function ajaxcomment(){

        $id = intval($_REQUEST['id']);
        $pege=intval($_GET['p']);
        if(empty($pege)){$pege=0;}
        $of=$pege*4;
        $field='jg_comment.id,jg_comment.content,jg_comment.createtime,jg_user.avatar,jg_user.username';
        $where='jg_Comment.`conid`='.$id.' and jg_Comment.`contype`=\'User\'';
        $data_comment=M('Comment')->field($field)->where($where)->join('jg_user ON jg_comment.userid = jg_user.id')->order('jg_comment.id desc')->limit($of.',4')->select();

        if($_GET['p']){
            if(empty($data_comment)){
                echo 1;
            }else {

                $p = $_GET['p'] + 1;
                $list = array('data_comment' => $data_comment, 'p' => $p, 'id' => $id);
                echo json_encode($list);//转换格式
            }

        }


    }



    public function qqcallback()
    {
        $jgqq=M("Oauth")->where("title='qq'")->find();
        import ( "@.Oauth.QQ.qqConnectAPI" );
        $qc = new QC($jgqq["appkey"],$jgqq["appid"]);
        $qqcallbackstr=$qc->qq_callback();
        $qqopenid=$qc->get_openid();

        $this->assign('openid',$qqopenid);
        $qc = new QC($qqcallbackstr,$qqopenid);
        $qquserinfo = $qc->get_user_info();
        $this->assign('qquser',$qquserinfo);
        $qquser=M("User")->where("openid='".$qqopenid."'")->find();

        if($qquser){
            $cookietime =  0;


            $jgsat_auth_key = sysmd5($this->sysConfig['ADMIN_ACCESS'].$_SERVER['HTTP_USER_AGENT']);
            $jgsat_auth = authcode($qquser['id']."-".$qquser['groupid']."-".$qquser['password'], 'ENCODE', $jgsat_auth_key);

            cookie('auth',$jgsat_auth,$cookietime);
            cookie('username',$qquser['username'],$cookietime);
            cookie('groupid',$qquser['groupid'],$cookietime);
            cookie('userid',$qquser['id'],$cookietime);
            cookie('email',$qquser['email'],$cookietime);

            //保存登录信息
            $dao = M('User');
            $data = array();
            $data['id']	=$authInfo['id'];
            $data['last_logintime']=time();
            $data['last_ip']= get_client_ip();
            $data['login_count']=array('exp','login_count+1');
            $dao->save($data);
            $this->assign('closeWin',1);
            $this->assign('jumpUrl',URL('Home-Index/index'));
            $this->success(L('login_ok'));
//            echo '<script language="javascript">';
//            echo 'window.close("登录成功！");';
//            echo 'window.opener=null;window.open("","_self");window.close();';
//            echo '</script>';
        }
        else{
            if($this->_userid){
                $user=M("User")->where("id=".$this->_userid)->find();
                $this->assign('user',$user);
                $this->display("Oauth:binding");
            }
            else{
                $this->display("Oauth:register");
            }
        }
    }

        //print_r($arr);exit;






    //简单实现json到php数组转换功能
    private function simple_json_parser($json){
        $json = str_replace("{","",str_replace("}","", $json));
        $jsonValue = explode(",", $json);
        $arr = array();
        foreach($jsonValue as $v){
            $jValue = explode(":", $v);
            $arr[str_replace('"',"", $jValue[0])] = (str_replace('"', "", $jValue[1]));
        }
        return $arr;
    }


}
?>